from .view import NotifyView
from . import schema
from fastapi import APIRouter, Depends, Security
from package.connector.sql_db import db
from package.fastapi import permission

router_notify = APIRouter(prefix='/notify', tags=['通告配置'])
router_meta = {'module': '系统管理', 'sub_module': '通告配置'}


@router_notify.get('/email', response_model=schema.PageEmail, openapi_extra=router_meta, name='email_get',
                   summary='获取邮件通告通道')
def email_get(q: str = None,
              page: int = 1,
              page_size: int = 10,
              order_by: str = 'created_at desc',
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['email:read'])):
    return NotifyView.get_page_items(session, q=q, filter_=[NotifyView.type == 'email'], page=page, page_size=page_size,
                                 order_by=order_by)


@router_notify.post('/email',
                    response_model=schema.EmailNotifyOut,
                    openapi_extra=router_meta, name='email_post',
                    summary='添加邮件通告通道')
def email_post(body: schema.EmailNotifyIn,
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['email:write'])):
    return NotifyView.create_item(session, {**body.dict(), 'type': 'email'})


@router_notify.patch('/email/{id}',
                     response_model=schema.EmailNotifyOut,
                     openapi_extra=router_meta,
                     name='email_patch', summary='修改邮件通告通道')
def email_patch(id: str,
                body: schema.EmailNotifyEdit,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['email:write'])):
    return NotifyView.update_item(session, filter_=[NotifyView.id == id, NotifyView.type == 'email'], update=body.dict())


@router_notify.delete('/email', openapi_extra=router_meta, name='email_delete',
                      summary='删除邮件通告通道')
def email_delete(body: schema.BatchNotifyId,
                 session=Depends(db.get_session),
                 user=Security(permission.security, scopes=['email:write'])):
    return NotifyView.delete_items(session, filter_=[NotifyView.id.in_(body.ids)])


@router_notify.post('/email/verify', openapi_extra=router_meta, name='email_verify',
                    summary='验证邮件通告配置')
def email_verify(body: schema.EmailVerify,
                 user=Security(permission.security, scopes=[])):
    return NotifyView.verify(type='email', configure=body.configure.dict())


# -------wechat-------------
@router_notify.get('/wechat', response_model=schema.PageWeChat, openapi_extra=router_meta, name='wechat_get',
                   summary='获取微信通告通道')
def wechat_get(q: str = None,
               page: int = 1,
               page_size: int = 10,
               order_by: str = 'created_at desc',
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['wechat:read'])):
    return NotifyView.get_page_items(session, q=q, filter_=[NotifyView.type == 'wechat'], page=page, page_size=page_size,
                                 order_by=order_by)


@router_notify.post('/wechat',
                    response_model=schema.WeChatNotifyOut,
                    openapi_extra=router_meta, name='wechat_post',
                    summary='添加微信通告通道')
def wechat_post(body: schema.WeChatNotifyIn,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['wechat:write'])):
    return NotifyView.create_item(session, {**body.dict(), 'type': 'wechat'})


@router_notify.patch('/wechat/{id}',
                     response_model=schema.WeChatNotifyOut,
                     openapi_extra=router_meta,
                     name='wechat_patch', summary='修改微信通告通道')
def wechat_patch(id: str,
                 body: schema.WeChatNotifyEdit,
                 session=Depends(db.get_session),
                 user=Security(permission.security, scopes=['wechat:write'])):
    return NotifyView.update_item(session, filter_=[NotifyView.id == id, NotifyView.type == 'wechat'], update=body.dict())


@router_notify.delete('/wechat', openapi_extra=router_meta, name='wechat_delete',
                      summary='删除微信通告通道')
def wechat_delete(body: schema.BatchNotifyId,
                  session=Depends(db.get_session),
                  user=Security(permission.security, scopes=['wechat:write'])):
    return NotifyView.delete_items(session, filter_=[NotifyView.id.in_(body.ids)])


@router_notify.post('/wechat/verify', openapi_extra=router_meta, name='wechat_verify',
                    summary='验证微信通告配置')
def wechat_verify(body: schema.WeChatVerify,
                  user=Security(permission.security, scopes=[])):
    return NotifyView.verify(type='wechat', configure=body.configure.dict())


# -------script-------------
@router_notify.get('/script', response_model=schema.PageScript, openapi_extra=router_meta, name='script_get',
                   summary='获取脚本通告通道')
def script_get(q: str = None,
               page: int = 1,
               page_size: int = 10,
               order_by: str = 'created_at desc',
               session=Depends(db.get_session),
               user=Security(permission.security, scopes=['script:read'])):
    return NotifyView.get_page_items(session, q=q, filter_=[NotifyView.type == 'script'], page=page, page_size=page_size,
                                 order_by=order_by)


@router_notify.post('/script',
                    response_model=schema.ScriptNotifyOut,
                    openapi_extra=router_meta, name='script_post',
                    summary='添加脚本通告通道')
def script_post(body: schema.ScriptNotifyIn,
                session=Depends(db.get_session),
                user=Security(permission.security, scopes=['script:write'])):
    return NotifyView.create_item(session, {**body.dict(), 'type': 'script'})


@router_notify.patch('/script/{id}',
                     response_model=schema.ScriptNotifyOut,
                     openapi_extra=router_meta,
                     name='script_patch', summary='修改脚本通告通道')
def script_patch(id: str,
                 body: schema.ScriptNotifyEdit,
                 session=Depends(db.get_session),
                 user=Security(permission.security, scopes=['script:write'])):
    return NotifyView.update_item(session, filter_=[NotifyView.id == id, NotifyView.type == 'script'], update=body.dict())


@router_notify.delete('/script', openapi_extra=router_meta, name='script_delete',
                      summary='删除脚本通告通道')
def script_delete(body: schema.BatchNotifyId,
                  session=Depends(db.get_session),
                  user=Security(permission.security, scopes=['script:write'])):
    return NotifyView.delete_items(session, filter_=[NotifyView.id.in_(body.ids)])


@router_notify.post('/script/verify', openapi_extra=router_meta, name='script_verify',
                    summary='验证脚本通告配置')
def script_verify(body: schema.ScriptVerify,
                  user=Security(permission.security, scopes=[])):
    return NotifyView.verify(type='script', configure=body.configure.dict())


# -------dingtalk-------------
@router_notify.get('/dingtalk', response_model=schema.PageDingTalk, openapi_extra=router_meta, name='dingtalk_get',
                   summary='获取钉钉通告通道')
def dingtalk_get(q: str = None,
                 page: int = 1,
                 page_size: int = 10,
                 order_by: str = 'created_at desc',
                 session=Depends(db.get_session),
                 user=Security(permission.security, scopes=['dingtalk:read'])):
    return NotifyView.get_page_items(session, q=q, filter_=[NotifyView.type == 'dingtalk'], page=page, page_size=page_size,
                                 order_by=order_by)


@router_notify.post('/dingtalk',
                    response_model=schema.DingTalkNotifyOut,
                    openapi_extra=router_meta, name='dingtalk_post',
                    summary='添加钉钉通告通道')
def dingtalk_post(body: schema.DingTalkNotifyIn,
                  session=Depends(db.get_session),
                  user=Security(permission.security, scopes=['dingtalk:write'])):
    return NotifyView.create_item(session, {**body.dict(), 'type': 'dingtalk'})


@router_notify.patch('/dingtalk/{id}',
                     response_model=schema.DingTalkNotifyOut,
                     openapi_extra=router_meta,
                     name='dingtalk_patch', summary='修改钉钉通告通道')
def dingtalk_patch(id: str,
                   body: schema.DingTalkNotifyEdit,
                   session=Depends(db.get_session),
                   user=Security(permission.security, scopes=['dingtalk:write'])):
    return NotifyView.update_item(session, filter_=[NotifyView.id == id, NotifyView.type == 'dingtalk'], update=body.dict())


@router_notify.delete('/dingtalk', openapi_extra=router_meta, name='dingtalk_delete',
                      summary='删除钉钉通告通道')
def dingtalk_delete(body: schema.BatchNotifyId,
                    session=Depends(db.get_session),
                    user=Security(permission.security, scopes=['dingtalk:write'])):
    return NotifyView.delete_items(session, filter_=[NotifyView.id.in_(body.ids)])


@router_notify.post('/dingtalk/verify', openapi_extra=router_meta, name='dingtalk_verify',
                    summary='验证钉钉通告配置')
def dingtalk_verify(body: schema.DingTalkVerify,
                    user=Security(permission.security, scopes=[])):
    return NotifyView.verify(type='dingtalk', configure=body.configure.dict())

